﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmCustomers : ICommonForm
    {
        private readonly CustomersMng _customersMng = new CustomersMng();
        private readonly SoldItemsMng _soldItemsMng = new SoldItemsMng();
        private string id;
        public frmCustomers()
        {
            InitializeComponent();
        }
        public frmCustomers(string _id)
        {
            InitializeComponent();
            this.id = _id;
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_customersMng.UpdateItem(txtID.Text.Trim(), txtName.Text.Trim(), txtPhone.Text.Trim(), txtAddress.Text.Trim(), txtEmail.Text.Trim(), txtDesc.Text.Trim(), rbIsVip.Checked, txtVipCode.Text.Trim()))
            {
                MessageBox.Show("Cập nhật thành công!", "Chú ý");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa thông tin khách hàng hiện tại không?", "Chú ý", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            if (_customersMng.DeleteItem(txtID.Text.Trim()))
            {
                MessageBox.Show("Xóa thông tin khách hàng thành công!", "Chú ý");
                ClearTextbox();
            }
        }
        private void LoadData(string id)
        {
            var item = _customersMng.GetOne(id);
            if (item == null)
            {
                MessageBox.Show("Không tìm thấy mã khách hàng này. Xin vui lòng kiểm tra lại", "Chú ý");
                return;
            }
            txtID.Text = item.ID;
            txtName.Text = item.Name;
            txtPhone.Text = item.PhoneNo;
            txtEmail.Text = item.Email;
            txtAddress.Text = item.Address;
            txtDesc.Text = item.Description;
            lblNoInvoice.Text = string.IsNullOrEmpty(Convert.ToString(item.NumberOfInvoice)) ? "0" : Convert.ToString(item.NumberOfInvoice);
            BindDataGridByCustomerId(item.ID);
            txtVipCode.Enabled = rbIsVip.Checked = Convert.ToBoolean(item.IsVip);
            rbNotVip.Checked = !rbIsVip.Checked;
            txtVipCode.Text = item.VipCode;
        }
        private void BindDataGridByCustomerId(string cusId)
        {
            var src = _soldItemsMng.GetListItemsByCustomerId(cusId);
            var cus = _customersMng.GetOne(cusId);
            if (src.Count <= 0)
            {
                dataGridView1.DataSource = null;
                lblTotalQuantity.Text = "0";
                lblTotalBuying.Text = "0";
                return;
            }
            dataGridView1.DataSource = src;

            // total buying cost and quantity
            decimal totalQty = 0;
            decimal totalBuyingCost = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                totalQty += Convert.ToDecimal(row.Cells[2].Value);
                totalBuyingCost += Convert.ToDecimal(row.Cells[4].Value);
            }
            lblTotalQuantity.Text = Convert.ToString(totalQty);
            lblTotalBuying.Text = Utilities.FormatNumber(Convert.ToDecimal(cus.TotalBuyingCost), Utilities.PrecisionFormat);
        }

        private void ClearTextbox()
        {
            txtPhone.Text =
            txtName.Text =
            txtID.Text =
            txtEmail.Text =
            txtDesc.Text =
            txtAddress.Text = "";
            lblNoInvoice.Text =
            lblTotalBuying.Text = "0";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtID.Text.Trim()))
                LoadData(txtID.Text.Trim());
        }

        private void frmCustomers_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            if (!string.IsNullOrEmpty(id))
                LoadData(id);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            var frm = new frmCustomersList() { MdiParent = frmMain.ActiveForm, WindowState = FormWindowState.Maximized };
            frm.Show();
            Close();
        }

        private void rbIsVip_CheckedChanged(object sender, EventArgs e)
        {
            txtVipCode.Enabled = rbIsVip.Checked;
        }
    }
}
